package com.lifeonwalden.ms.web.controller;

import com.lifeonwalden.ms.web.bean.Person;
import com.lifeonwalden.ms.web.bean.Response;
import com.lifeonwalden.ms.web.util.ResponseUtil;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@RestController
@RequestMapping("/auth")
public class FormController {

    @GetMapping("form/newPersonGet")
    public Response newPersonGet(@ModelAttribute Person person) {
        return ResponseUtil.success(person);
    }

    @PostMapping("form/newPersonPost")
    public Response newPersonPost(@RequestBody Person person) {
        return ResponseUtil.success(person);
    }

    @GetMapping("form/getPerson")
    public Response getPerson() {
        return ResponseUtil.success(new Person().setName("ZhangSan").setAge(18).setBirthday(new java.util.Date("2022/08/09"))
                .setChildren(Arrays.asList(new Person().setAge(5).setName("MaFen").setBirthday(new java.util.Date("2022/09/17")))));
    }

    @GetMapping("form/getException")
    public Response getException() {
        throw new RuntimeException();
    }

    @GetMapping("form/getFailed")
    public Response getFailed() {
        return ResponseUtil.fail();
    }

    @GetMapping("form/get403")
    public void get403(HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        try {
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("form/get500")
    public void get500(HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        try {
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("form/get404")
    public void get404(HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        try {
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("form/get401")
    public void get401(HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        try {
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
